<template>
  <div class="confirm-order">
    <!-- 头部 -->
    <div class="header">
      <div class="back-button" @click="goBack">
        <span class="back-arrow">‹</span>
      </div>
      <h1 class="title">确认订单</h1>
    </div>

    <!-- 收货地址 -->
    <div class="address-section">
      <div class="address-content">
        <div class="address-icon">📍</div>
        <div class="address-info">
          <div class="address-text">{{ selectedAddress || '请选择收货地址' }}</div>
        </div>
        <div class="change-link" @click="showAddressMap = true">更改</div>
      </div>
    </div>

    <!-- 配送信息 -->
    <div class="delivery-info">
      订单将于明天18:00前送达
    </div>

    <!-- 商品列表 -->
    <div class="products-section">
      <div class="product-item" v-for="item in orderItems" :key="item.id">
        <div class="product-image">
          <img :src="item.image" :alt="item.name" />
        </div>
        <div class="product-info">
          <div class="product-name">{{ item.name }}</div>
          <div class="product-spec">{{ item.specification }}</div>
          <div class="product-quantity">
            <span class="quantity-icon">🛍️</span>
            <span class="quantity-text">数量: {{ item.quantity.toString().padStart(2, '0') }}</span>
          </div>
          <div class="product-price">¥{{ item.price }}</div>
        </div>
      </div>
    </div>



    <!-- 支付方式 -->
    <div class="payment-section" @click="selectPayment">
      <div class="payment-icon">💳</div>
      <div class="payment-text">支付方式</div>
      <div class="payment-value">
        <span class="payment-name">支付宝</span>
        <span class="arrow">›</span>
      </div>
    </div>

    <!-- 备注信息 -->
    <div class="remarks-section">
      <div class="remarks-title">备注信息</div>
      <div class="remarks-input-container">
        <textarea 
          v-model="remarks" 
          class="remarks-input" 
          placeholder="在此处输入您的备注信息"
          maxlength="225"
        ></textarea>
        <div class="char-counter">{{ remarks.length }}/225</div>
      </div>
    </div>

    <!-- 订单摘要 -->
    <div class="order-summary">
      <div class="summary-item">
        <span class="summary-label">小计</span>
        <span class="summary-value">¥{{ subtotal }}</span>
      </div>

      <div class="summary-item total">
        <span class="summary-label">总计</span>
        <span class="summary-value">¥{{ totalPrice }}</span>
      </div>
    </div>

    <!-- 确认支付按钮 -->
    <div class="bottom-button">
      <button class="confirm-payment-btn" @click="confirmPayment">
        确认支付
      </button>
    </div>

    <!-- 地址选择组件 -->
    <AddressMap
      v-model:visible="showAddressMap"
      @select-address="handleAddressSelect"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import AddressMap from '@/components/AddressMap.vue'

const router = useRouter()

// 订单商品数据
const orderItems = ref<any[]>([])

// 备注信息
const remarks = ref('')

// 地址相关
const showAddressMap = ref(false)
const selectedAddress = ref('')

// 从购物车读取数据
const loadOrderData = () => {
  const cartItems = localStorage.getItem('cartItems')
  if (cartItems) {
    orderItems.value = JSON.parse(cartItems)
  } else {
    // 默认数据
    orderItems.value = [
      {
        id: 1,
        name: '碳酸钙D3维钙营养行健六顺·朗迪',
        specification: '片剂 60片',
        price: '12.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcwt.svg'
      },
      {
        id: 2,
        name: '碳酸钙D3维钙营养行健六顺·朗迪',
        specification: '片剂 60片',
        price: '12.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcz0.svg'
      },
      {
        id: 3,
        name: '碳酸钙D3维钙营养行健六顺·朗迪',
        specification: '片剂 60片',
        price: '12.00',
        quantity: 2,
        image: '/src/views/sow/imgs/v2_sypcxi.svg'
      }
    ]
  }
}

// 计算小计
const subtotal = computed(() => {
  return orderItems.value.reduce((sum, item) => {
    return sum + (parseFloat(item.price) * item.quantity)
  }, 0).toFixed(0)
})

// 计算总价
const totalPrice = computed(() => {
  return subtotal.value
})

// 方法
const goBack = () => {
  router.back()
}

// 处理地址选择
const handleAddressSelect = (address: any) => {
  selectedAddress.value = address.address
  localStorage.setItem('deliveryAddress', JSON.stringify(address))
}

const selectPayment = () => {
  console.log('选择支付方式')
}

const confirmPayment = () => {
  if (!selectedAddress.value) {
    showToast('请选择收货地址')
    return
  }
  console.log('确认支付')
  // 跳转到订单详情页面
  router.push('/order-details')
}

// 轻提示
const showToast = (message: string) => {
  const toast = document.createElement('div')
  toast.className = 'toast-message'
  toast.textContent = message
  document.body.appendChild(toast)
  
  setTimeout(() => toast.classList.add('show'), 10)
  setTimeout(() => {
    toast.classList.remove('show')
    setTimeout(() => {
      if (document.body.contains(toast)) {
        document.body.removeChild(toast)
      }
    }, 300)
  }, 3000)
}

onMounted(() => {
  loadOrderData()

  // 加载保存的地址
  const savedAddress = localStorage.getItem('deliveryAddress')
  if (savedAddress) {
    try {
      const address = JSON.parse(savedAddress)
      selectedAddress.value = address.address
    } catch (error) {
      console.error('解析地址数据失败:', error)
    }
  }
})
</script>

<style scoped>
.confirm-order {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 100px;
}

/* 头部 */
.header {
  position: relative;
  background: white;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e0e0e0;
}

.back-button {
  position: absolute;
  left: 20px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.back-arrow {
  font-size: 24px;
  color: #333;
  font-weight: 500;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

/* 收货地址 */
.address-section {
  background: white;
  margin: 12px 0;
  padding: 16px 20px;
}

.address-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.address-icon {
  font-size: 18px;
  color: #4A90E2;
}

.address-info {
  flex: 1;
}

.address-text {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
}

.change-link {
  font-size: 14px;
  color: #4A90E2;
  cursor: pointer;
}

/* 配送信息 */
.delivery-info {
  background: white;
  padding: 12px 20px;
  font-size: 14px;
  color: #666;
  border-bottom: 1px solid #e0e0e0;
}

/* 商品列表 */
.products-section {
  background: white;
  margin: 12px 0;
}

.product-item {
  display: flex;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.product-item:last-child {
  border-bottom: none;
}

.product-image {
  width: 60px;
  height: 60px;
  margin-right: 12px;
  flex-shrink: 0;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 4px;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.product-name {
  font-size: 14px;
  color: #333;
  font-weight: 500;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.product-spec {
  font-size: 12px;
  color: #666;
}

.product-quantity {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #666;
  margin-top: 2px;
}

.quantity-icon {
  font-size: 12px;
  opacity: 0.8;
}

.quantity-text {
  font-size: 12px;
  font-weight: 500;
}

.product-price {
  font-size: 14px;
  color: #333;
  font-weight: 600;
  margin-top: auto;
}



/* 支付方式 */
.payment-section {
  background: white;
  margin: 12px 0;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}

.payment-icon {
  font-size: 18px;
  color: #4CAF50;
}

.payment-text {
  flex: 1;
  font-size: 14px;
  color: #333;
}

.payment-value {
  display: flex;
  align-items: center;
  gap: 8px;
}

.payment-name {
  font-size: 14px;
  color: #4A90E2;
}

/* 备注信息 */
.remarks-section {
  background: white;
  margin: 12px 0;
  padding: 16px 20px;
}

.remarks-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 12px;
  font-weight: 500;
}

.remarks-input-container {
  position: relative;
}

.remarks-input {
  width: 100%;
  min-height: 80px;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  color: #333;
  resize: none;
  outline: none;
  font-family: inherit;
}

.remarks-input::placeholder {
  color: #999;
}

.char-counter {
  position: absolute;
  bottom: 8px;
  right: 12px;
  font-size: 12px;
  color: #999;
}

/* 订单摘要 */
.order-summary {
  background: white;
  margin: 12px 0;
  padding: 16px 20px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.summary-item:last-child {
  margin-bottom: 0;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.summary-label {
  font-size: 14px;
  color: #333;
}

.summary-value {
  font-size: 14px;
  color: #333;
  font-weight: 600;
}



.summary-item.total .summary-label,
.summary-item.total .summary-value {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

/* 底部按钮 */
.bottom-button {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px 20px;
  background: white;
  border-top: 1px solid #e0e0e0;
  z-index: 1000;
}

.confirm-payment-btn {
  width: 100%;
  background: #4A90E2;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.confirm-payment-btn:hover {
  background: #357ABD;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .header {
    padding: 14px 16px;
  }
  
  .back-button {
    left: 16px;
  }
  
  .title {
    font-size: 16px;
  }
  
  .address-section,
  .products-section,
  .payment-section,
  .remarks-section,
  .order-summary {
    margin: 8px 0;
  }
  
  .address-section,
  .payment-section,
  .remarks-section,
  .order-summary {
    padding: 14px 16px;
  }
  
  .product-item {
    padding: 14px 16px;
  }
  
  .bottom-button {
    padding: 12px 16px;
  }
  
  .confirm-payment-btn {
    padding: 14px;
    font-size: 15px;
  }
}

/* Toast 样式 */
.toast-message {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s;
}

.toast-message.show {
  opacity: 1;
}
</style> 